package com.spark.sql

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

object DEmo15SParkJDBC {
  def main(args: Array[String]): Unit = {

    /**
     * 使用jdbc链接spark sql jdbc服务
     *
     */

    //1、加载驱动
    Class.forName("org.apache.hive.jdbc.HiveDriver")

    //2、创建jdbc连接
    val con: Connection = DriverManager.getConnection("jdbc:hive2://node03:10000/bigdata")

    //3、写sql查询数据
    val stat: PreparedStatement = con.prepareStatement("select clazz,count(1) as c from student group by clazz")

    //4、执行查询
    val resultSet: ResultSet = stat.executeQuery()

    //5、解析数据
    while (resultSet.next()) {
      val clazz: String = resultSet.getString("clazz")
      val c: Long = resultSet.getLong("c")
      println(s"$clazz\t$c")
    }

    //关闭链接
    stat.close()
    con.close()


  }

}
